﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Models;
using MySql.Data.MySqlClient;
using Common;

namespace DAL
{
   public  class HanderDAL
    {
       //查询所有
       public DataTable HanderListAll(string handername) {
           string sql = string.Empty;
           if (!string.IsNullOrWhiteSpace(handername))
           {
               sql += " and hname like '%" + handername + "%'";
           }
           return SqlHelper.ExecuteDataTable("select * from hander where isdel =0 " + sql + " order by edittime desc");
       }
       //转换拉手模板
       public Hander ToHander(DataRow dr ) {
           Hander h = new Hander();
           h.Hid = Convert.ToInt32(dr["hid"]);
           h.Hname = dr["hname"].ToString();
           h.Hprice = Convert.ToDecimal(dr["hprice"]);
           return h;
       }
       //根据id获取
       public Hander GetHanderById(string id) {
           DataTable dt = SqlHelper.ExecuteDataTable("select * from hander where isdel=0 and hid=@hid",new MySqlParameter("@hid",id));
           if (dt.Rows.Count == 1)
           {
               return ToHander(dt.Rows[0]);
           }
           else {
               throw new Exception("存在多行重复数据，请联系管理员");
           }
       }
       //新增
       public int SaveData(Hander h ) {
           return SqlHelper.ExecuteNonQuery("insert into hander(hname,hprice,isdel,adduser,edituser,addtime,edittime) values(@hname,@hprice,@isdel,@adduser,@edituser,@addtime,@edittime)",
               new MySqlParameter("@hname",h.Hname),new MySqlParameter("@hprice",h.Hprice),new MySqlParameter("isdel","0"),new MySqlParameter("@adduser",RemeberName.Username),
               new MySqlParameter("@edituser", RemeberName.Username),new MySqlParameter("@addtime",DateTime.Now.ToString()),new MySqlParameter("@edittime",DateTime.Now.ToString()));

       }
       //编辑
       public int UpdateData(Hander h) {
           return SqlHelper.ExecuteNonQuery("update hander set hname=@hname,hprice=@hprice,edittime=@edittime,edituser=@edituser where hid=@hid",
               new MySqlParameter("@hname",h.Hname),new MySqlParameter("@hprice",h.Hprice),new MySqlParameter("@edittime",DateTime.Now.ToString()),
                   new MySqlParameter("@edituser", RemeberName.Username), new MySqlParameter("@hid",h.Hid));
       }
       //删除数据
       public int DelById(string id)
       {
           return SqlHelper.ExecuteNonQuery("update hander set isdel=1 ,edittime=@edittime,edituser=@edituser where hid=@hid",
               new MySqlParameter("@edittime", DateTime.Now.ToString()), new MySqlParameter("@edituser", RemeberName.Username), new MySqlParameter("@hid", id)); ;
       }
    }
}
